﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vds2465Telegrams;

namespace Vds2465Parser
{
	/// <summary>
	/// this class takes care of generating and parsing bytes for the calling - variable length - SEND_NDAT telegram
	/// </summary>
	internal static class GenParse_SEND_NDAT
	{
		internal static byte[] GenerateBytes(Telegram_Calling_SEND_NDAT telegramCallingSENDNDAT)
		{			
			byte controlField = ControlField.Generate_ControlField_Calling(telegramCallingSENDNDAT.Direction,
						telegramCallingSENDNDAT.FrameCountValid, telegramCallingSENDNDAT.FrameCountFlag, telegramCallingSENDNDAT.TelegramFunction);

			byte addressByte = telegramCallingSENDNDAT.Address;

			byte[] userDataBytes = GenParse_UserData.GenerateBytes4UserDataColl(telegramCallingSENDNDAT.GetCopyOfUserDataColl());
			byte lengthValue = (byte) ((byte) 2 + (byte) userDataBytes.Length);

			// 6 = header with start byte, 2 length bytes, separator byte, control field and address field
			// 2 = checksum and end byte
			int lengthOfCompleteTelegram = 6 + userDataBytes.Length + 2;

			byte[] telegramBytes = new byte[lengthOfCompleteTelegram];

			telegramBytes[0] = VariableLength.StartByteValue;
			telegramBytes[1] = lengthValue;
			telegramBytes[2] = lengthValue;
			telegramBytes[3] = VariableLength.AfterLengthByteValue;
			telegramBytes[VariableLength.PositionOfControlField] = controlField;
			telegramBytes[VariableLength.PositionOfAddressByte] = addressByte;

			for (int userDataBytesIndex = 0; userDataBytesIndex < userDataBytes.Length; userDataBytesIndex++)
			{
				telegramBytes[6 + userDataBytesIndex] = userDataBytes[userDataBytesIndex];
			}

			// get bytes for calculation of checksum
			byte[] checksumBytes = new byte[userDataBytes.Length + 2];
			checksumBytes[0] = controlField;
			checksumBytes[1] = addressByte;
			for (int index = 0; index < userDataBytes.Length; index++)
			{
				checksumBytes[index + 2] = userDataBytes[index];
			}

			telegramBytes[lengthOfCompleteTelegram - 2] = GenParse_Checksum.CalculateChecksum(checksumBytes);
			telegramBytes[lengthOfCompleteTelegram - 1] = VariableLength.EndByteValue;

			return telegramBytes;
		}

		internal static Telegram_Calling_SEND_NDAT ParseBytes(byte[] telegramBytes, eDirection direction, bool frameCountValid,	bool frameCountFlag)
		{
			byte address = telegramBytes[VariableLength.PositionOfAddressByte];

			byte[] userDataBytes = VariableLength.GetUserDataBytes(telegramBytes);
			if (userDataBytes != null)
			{
				UserDataColl userDataColl = GenParse_UserData.ParseBytes4UserDataColl(userDataBytes);
				if (userDataColl != null)
				{
					//foreach (UserData userData in userDataColl)
					//{
					//    userDataColl.Add(userData);
					//}

					Telegram_Calling_SEND_NDAT telegramCallingSendNdat = new Telegram_Calling_SEND_NDAT(direction, address, frameCountValid, frameCountFlag, userDataColl);
					return telegramCallingSendNdat;
				}
				else
				{
					// TODO handle error
				}
			}
			else
			{
				// TODO handle error
			}

			return null;
		}
	}
}
